package com.survey.action;

import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.survey.bean.News;
import com.survey.util.Constant;
import com.survey.util.DateUtil;
import com.survey.util.FieldUtil;
import com.survey.util.MessageUtil;
import com.survey.util.Page;
import com.survey.util.common.BaseAction;
import com.survey.service.NewsService;

@Controller
@RequestMapping("/sys")
public class NewsAction extends BaseAction {
    private String			actionname	= "公告";
    private String			actionclass	= "News";
    @Autowired
    private NewsService	service;

    @RequestMapping(value = "/add2News.do")
    public String add2() {
        request.setAttribute("actionname", actionname);
        request.setAttribute("actionclass", actionclass);
        return "addNews";
    }

    @RequestMapping(value = "/getNews.do")
    public String get(int uid) {
        try {
            News temp = service.get(uid);
            request.setAttribute("modifybean", temp);

            request.setAttribute("actionname", actionname);
            request.setAttribute("actionclass", actionclass);
            return "modifyNews";
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.addMessage(request, "获取信息失败.");
            return ERROR;
        }
    }

    @RequestMapping(value = "/addNews.do")
    public String add(News bean) {
        try {
            bean.setAddDate(DateUtil.getCurrentTime(DateUtil.FULL));
            service.add(bean);
            MessageUtil.addMessage(request, "添加成功.");
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.addMessage(request, "添加失败.");
            return ERROR;
        }

    }

    @RequestMapping(value = "/updateNews.do")
    public String update(News bean) {
        try {
            bean.setImgFile(getUpdateFileField("imgFile"));
            bean.setAddDate(DateUtil.getCurrentTime(DateUtil.FULL));
            service.update(bean);
            MessageUtil.addMessage(request, "更新成功.");
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.addMessage(request, "更新失败.");
            return ERROR;
        }
    }

    @RequestMapping(value = "/deleteNews.do")
    public String delete(String ids) {
        try {
            service.deleteAll(ids);
            MessageUtil.addRelMessage(request, "操作成功.", "mainquery");
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.addMessage(request, "操作失败.");
            return ERROR;
        }
    }

    @RequestMapping(value = "/queryNews.do")
    public String query() {
        try {
            // 字段名称集合
            LinkedList<String> parmnames = new LinkedList<String>();
            // 字段值集合
            LinkedList<Object> parmvalues = new LinkedList<Object>();
            Page p = FieldUtil.createPage(request, News.class, parmnames, parmvalues);

            Page page = service.selectPage(p, News.class);
            session.setAttribute(Constant.SESSION_PAGE, page);

            request.setAttribute("actionname", actionname);
            request.setAttribute("actionclass", actionclass);
            return "listNews";
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }

}
